﻿var DashboardManager = function (managerContainer) {
    var self = this;

    var Elements = {
        cereriInitiate: managerContainer.find('.cereriInitiate'),
        cereriInitiateUrgente: managerContainer.find('.cereriInitiateUrgente'),
        angajati: managerContainer.find('.angajati'),
        cereriContainer: managerContainer.find('.cereriContainer'),
        subordonatiContainer: managerContainer.find('.subordonatiContainer')
    };

    this.refresh = function (callback) {
        app.TMMService.TMM.GetDashboardData_CereriConcedii(function (response) {
            if (response.Exception) {
            } else {
                response = response.FinalResult;

                if (app.User.Rights.ALL || app.User.Rights.AprobareCereriConcedii || app.User.Rights.RefuzareCereriConcedii) {
                    Elements.cereriInitiate.find(".box_content").html(response.NrInitiate);
                    var descr = response.NrInitiate == 1 ? translate('Dashboard_DashboardManager_CerereInitiata') : translate('Dashboard_DashboardManager_CereriInitiate');
                    Elements.cereriInitiate.find(".box_description").html(descr);

                    if (response.NrInitiateUrgente)
                        Elements.cereriInitiateUrgente.show().html(response.NrInitiateUrgente);
                    else Elements.cereriInitiateUrgente.hide();

                    Elements.cereriContainer.show();
                } else Elements.cereriContainer.hide();

                Elements.angajati.find(".box_content").html(response.NrAngajati);
                descr = response.NrAngajati == 1 ? translate('General_Angajat') : translate('General_Angajati');
                Elements.angajati.find(".box_description").html(descr);

                Elements.subordonatiContainer.show();
            }

            managerContainer.trigger('visible');

            if (callback)
                callback();
        });
    };

    var initialize = function () {
        Elements.cereriInitiate.click(function (e) {
            if (!app.Pages.CereriConcedii.instance) {
                app.Pages.CereriConcedii.instance = new CereriConcediiPage(app.Pages.CereriConcedii.container);
                app.Pages.CereriConcedii.instance.initialize();
            }
            app.changePage(app.Pages.CereriConcedii);

            app.Pages.CereriConcedii.instance.setSearchData("");
            app.Pages.CereriConcedii.instance.setFilterData({
                statusConcediu: {
                    Id: 0,
                    Nume: translate('CereriConcediu_Cerere_Initiate')
                },
                ignoreSelf: true
            }, true);

            return Event.stopEvent(e);
        });

        Elements.cereriInitiateUrgente.click(function (e) {
            if (!app.Pages.CereriConcedii.instance) {
                app.Pages.CereriConcedii.instance = new CereriConcediiPage(app.Pages.CereriConcedii.container);
                app.Pages.CereriConcedii.instance.initialize();
            }
            app.changePage(app.Pages.CereriConcedii);

            app.Pages.CereriConcedii.instance.setSearchData("");
            app.Pages.CereriConcedii.instance.setFilterData({
                statusConcediu: {
                    Id: 0,
                    Nume: translate('CereriConcediu_Cerere_Initiate')
                },
                ignoreSelf: true
            }, true);

            return Event.stopEvent(e);
        });

        Elements.angajati.click(function (e) {
            if (!app.Pages.EmployeesList.instance)
                app.Pages.EmployeesList.instance = new EmployeesListPage(app.Pages.EmployeesList.container);
            app.changePage(app.Pages.EmployeesList);
            app.Pages.EmployeesList.instance.refresh(true);

            return Event.stopEvent(e);
        });

        $(document).bind('updateUnreadNotifications', function (e) {
            self.refresh();
        });
    };

    initialize();
};